package com.jjwxc.jwjskandriod.readActivity.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.view.PageLoader;
import com.jjwxc.jwjskandriod.readActivity.view.PageMode;
import com.jjwxc.jwjskandriod.readActivity.view.PageStyle;

public class ReadTextDialog extends Dialog {
    private PageLoader mPageLoader;
    private Activity mActivity;

    private int mBrightness;
    private int mTextSize;

    private boolean isTextDefault;
    private PageMode mPageMode;
    private PageStyle mPageStyle;
    private ReadSettingManager mSettingManager;

    TextView mTvFontMinus;
    TextView mTvFont;
    TextView mTvFontPlus;
    TextView tv_typeface;

    RadioGroup readSettingLineSpacing;

    LinearLayout ll_more_setting;
    LinearLayout ll_bg;
    LinearLayout ll_typeface;
    LinearLayout ll_autopage;

    RadioButton reader_icon_line_spacing_small;
    RadioButton reader_icon_line_spacing_middle;
    RadioButton reader_icon_line_spacing_big;
    RadioButton reader_icon_line_spacing_two;
    AutoPageState autoPageState;
    String[] ziti = {"系统字体", "思源宋体", "三极萌娃体", "思源黑体", "三极真喵体", "三极拙楷体"};
    public ReadTextDialog(@NonNull Activity activity, PageLoader mPageLoader, AutoPageState autoPageState) {
        super(activity);
        mActivity = activity;
        this.mPageLoader = mPageLoader;
        this.autoPageState = autoPageState;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_read_text);
        setUpWindow();
        initData();
        initWidget();
        initClick();
    }

    //设置Dialog显示的位置
    private void setUpWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.getDecorView().setBackground(null);
    }

    private void initData() {

        mSettingManager = ReadSettingManager.getInstance();

        mBrightness = mSettingManager.getBrightness();
        mTextSize = mSettingManager.getTextSize();
        isTextDefault = mSettingManager.isDefaultTextSize();
        mPageMode = mSettingManager.getPageMode();
        mPageStyle = mSettingManager.getPageStyle();

    }

    @SuppressLint("SetTextI18n")
    private void initWidget() {
        ll_typeface = findViewById(R.id.ll_typeface);
        tv_typeface = findViewById(R.id.tv_typeface);
        mTvFontMinus = findViewById(R.id.read_setting_tv_font_minus);
        mTvFont = findViewById(R.id.read_setting_tv_font);
        mTvFontPlus = findViewById(R.id.read_setting_tv_font_plus);
        readSettingLineSpacing = findViewById(R.id.read_setting_line_spacing);
        ll_more_setting = findViewById(R.id.ll_more_setting);
        ll_autopage = findViewById(R.id.ll_autopage);
        ll_bg = findViewById(R.id.ll_bg);
        reader_icon_line_spacing_small = findViewById(R.id.reader_icon_line_spacing_small);
        Drawable left = mActivity.getResources().getDrawable(R.drawable.read_line_select_five);
        left.setBounds(0, FFUtils.getPx(5), FFUtils.getPx(25), FFUtils.getPx(25));//必须设置图片的大小否则没有作用
        reader_icon_line_spacing_small.setCompoundDrawables(null, left, null, null);//设置图片left这里如果是右边就放到第二个参数里面依次对应

        reader_icon_line_spacing_middle = findViewById(R.id.reader_icon_line_spacing_middle);
        Drawable left1 = mActivity.getResources().getDrawable(R.drawable.read_line_select_four);
        left1.setBounds(0, FFUtils.getPx(5), FFUtils.getPx(25), FFUtils.getPx(25));//必须设置图片的大小否则没有作用
        reader_icon_line_spacing_middle.setCompoundDrawables(null, left1, null, null);//设置图片left这里如果是右边就放到第二个参数里面依次对应

        reader_icon_line_spacing_big = findViewById(R.id.reader_icon_line_spacing_big);
        Drawable left2 = mActivity.getResources().getDrawable(R.drawable.read_line_select_three);
        left2.setBounds(0, FFUtils.getPx(5), FFUtils.getPx(25), FFUtils.getPx(25));//必须设置图片的大小否则没有作用
        reader_icon_line_spacing_big.setCompoundDrawables(null, left2, null, null);//设置图片left这里如果是右边就放到第二个参数里面依次对应

        reader_icon_line_spacing_two = findViewById(R.id.reader_icon_line_spacing_two);
        Drawable left3 = mActivity.getResources().getDrawable(R.drawable.read_line_select_two);
        left3.setBounds(0, FFUtils.getPx(5), FFUtils.getPx(25), FFUtils.getPx(25));//必须设置图片的大小否则没有作用
        reader_icon_line_spacing_two.setCompoundDrawables(null, left3, null, null);//设置图片left这里如果是右边就放到第二个参数里面依次对应

        mTvFont.setText((mTextSize >> 1) + "");
        //暗黑模式
        if (mSettingManager.isNightMode()) {
            ll_bg.setBackgroundResource(R.drawable.shape_black);
        } else {
            ll_bg.setBackgroundResource(R.drawable.shape_while);
        }
        //第几个字体
        int pos = mSettingManager.getTypefacePos();

        tv_typeface.setText(ziti[pos]+" >");
    }

    private void changeFontSize(boolean inc) {
        int currentSize = Integer.parseInt(mTvFont.getText().toString());
        currentSize += (inc ? 1 : -1);
        if (currentSize < 7) {
            return;
        }
        int fontSize = currentSize << 1;
        mTvFont.setText("" + currentSize);
        mPageLoader.setTextSize(fontSize, mSettingManager.getTextInterval(), mSettingManager.getTypefacePath());
    }

    @SuppressLint("SetTextI18n")
    private void initClick() {
        ll_autopage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoPageState.onItemClick(true);
                dismiss();
            }
        });
        ll_typeface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TypefaceDialog(mActivity, mPageLoader, new TypefaceDialog.PopTypefaceListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onValueClick() {
                        //第几个字体
                        int pos = mSettingManager.getTypefacePos();

                        tv_typeface.setText(ziti[pos]+" >");
                    }
                }).show();
            }
        });
        //字体大小调节
        //减小字体
        mTvFontMinus.setOnClickListener(
                (v) -> {
//                    int fontSize = ScreenUtils.spToPx(Integer.parseInt(mTvFont.getText().toString()) - 1);
//                    if (fontSize < 0) return;
//                    mTvFont.setText(ScreenUtils.pxToSp(fontSize) + "");
//                    mPageLoader.setTextSize(fontSize, mSettingManager.getTextInterval(), mSettingManager.getTypefacePath());
                    changeFontSize(false);
                }
        );
        //增大字体
        mTvFontPlus.setOnClickListener(
                (v) -> {
//                    int fontSize = ScreenUtils.spToPx(Integer.parseInt(mTvFont.getText().toString()) + 1);
//                    mTvFont.setText(ScreenUtils.pxToSp(fontSize) + "");
//                    mPageLoader.setTextSize(fontSize, mSettingManager.getTextInterval(), mSettingManager.getTypefacePath());
                    changeFontSize(true);
                }
        );

        //排版调整
        readSettingLineSpacing.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int textInterval = 1;
                switch (checkedId) {
                    case R.id.reader_icon_line_spacing_small:
                        textInterval = 1;
                        break;
                    case R.id.reader_icon_line_spacing_middle:
                        textInterval = 2;
                        break;
                    case R.id.reader_icon_line_spacing_big:
                        textInterval = 3;
                        break;
                    case R.id.reader_icon_line_spacing_two:
                        textInterval = 4;
                        break;
                    default:
                        textInterval = 1;
                        break;
                }
                /*int fontSize = Integer.valueOf(mTvFont.getText().toString());
                mTvFont.setText(fontSize + "");
                mSettingManager.setTextInvterval(textInterval);
                mPageLoader.setTextSize(fontSize, textInterval, mSettingManager.getTypefacePath());*/

                int fontSize = Integer.valueOf(mTvFont.getText().toString());
                mTvFont.setText(fontSize + "");
                mSettingManager.setTextInvterval(textInterval);

                int currentSize = fontSize << 1;
                mPageLoader.setTextSize(currentSize, textInterval, mSettingManager.getTypefacePath());
            }
        });

        ll_more_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ReadTurningDialog(mActivity, mPageLoader).show();
            }
        });
    }

    public interface AutoPageState {
        void onItemClick(boolean state);
    }
}

